package com.shelpe.service.simbaproxy.filter;

import java.util.List;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.Debug;
import com.netflix.zuul.context.RequestContext;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ErrorFilter extends ZuulFilter {

	@Override
	public boolean shouldFilter() {
		return RequestContext.getCurrentContext().getThrowable() != null;
	}

	@Override
	public Object run() {
		RequestContext context = RequestContext.getCurrentContext();
		log.debug("catch exception: {}", context.getThrowable().getMessage());
		
		List<String> records = Debug.getRequestDebug();
		if(records != null){
			for(String record: records){
				log.info("request log: {}", record);
			}
		}
		records = Debug.getRoutingDebug();
		if(records != null){
			for(String record: records){
				log.info("routing log: {}", record);
			}
		}
		context.setResponseStatusCode(500);
		return null;
	}

	@Override
	public String filterType() {
		return "error";
	}

	@Override
	public int filterOrder() {
		return 0;
	}

}
